package com.summerbird.mallchat.common.user.service.adapter;

import com.summerbird.mallchat.common.chat.domain.entity.Room;
import com.summerbird.mallchat.common.chat.domain.entity.RoomFriend;
import com.summerbird.mallchat.common.chat.domain.entity.RoomGroup;
import com.summerbird.mallchat.common.common.domain.enums.NormalOrNoEnum;
import com.summerbird.mallchat.common.user.domain.entity.User;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

public class ChatAdapter {
    public static final String SEPARATOR = ",";

    public static String generateRoomKey(List<Long> uidList) {
        return uidList.stream()
                .sorted()
                .map(String::valueOf)
                .collect(Collectors.joining(SEPARATOR));
    }

    public static RoomFriend buildFriendRoom(Long roomId, List<Long> uidList) {
        List<Long> collect = uidList.stream().sorted().collect(Collectors.toList());
        RoomFriend insert = new RoomFriend();
        insert.setRoomId(roomId);
        insert.setRoomKey(generateRoomKey(uidList));
        insert.setUid1(collect.get(0));
        insert.setUid2(collect.get(1));
        insert.setStatus(NormalOrNoEnum.NORMAL.getStatus());
        return insert;
    }

    public static Set<Long> getFriendUidSet(Collection<RoomFriend> values, Long uid) {
        return values.stream()
                .map(a -> getFriendUid(a, uid))
                .collect(Collectors.toSet());
    }

    public static Long getFriendUid(RoomFriend a, Long uid) {
        return Objects.equals(a.getUid1(), uid) ? a.getUid2() : a.getUid1();
    }

    public static RoomGroup buildRoomGroup(User user, Room room) {
        RoomGroup roomGroup = new RoomGroup();
        roomGroup.setRoomId(room.getId());
        roomGroup.setAvatar(user.getAvatar());
        roomGroup.setName(user.getName() + "的群组");
        return roomGroup;
    }
}
